#!/usr/bin/env python
"""
# -*- coding: utf-8 -*-
# @Time    : 2020/9/22 11:23
# @Author  : AshJo
# @Desc    : 自定义pickle模块
# @Filename: custom_pickle.py
# @Version : v1.0
"""

import pickle


class Cource:
    def __init__(self, name, period, price):
        self.name = name
        self.period = period
        self.price = price


class Mypickle:
    def __init__(self, path):
        self.path = path

    def load(self):
        with open(self.path, 'rb') as f:
            while True:
                try:
                    yield pickle.load(f)
                except EOFError:
                    break

    def dump(self, obj):
        with open(self.path, 'ab') as f:
            pickle.dump(obj, f)


python = Cource('Python全栈22期', 8, 21960)
linux = Cource('Linux运维23期', 7, 19699)
go = Cource('Go开发22期', 6, 15999)

m1 = Mypickle('pickle_file')
m1.dump(linux)
m1.dump(go)
for i in m1.load():
    print(i.name)
